﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
namespace AnyOffice.general.system.workflow.flow_type
{
    public partial class type_edit : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = num;
                DataTable table = new FlowSortDao().SelectAll(base.Conn);
                this.ddlSortId.DataSource = table;
                this.ddlSortId.DataValueField = "SortId";
                this.ddlSortId.DataTextField = "SortName";
                this.ddlSortId.DataBind();
                DataTable table2 = new FlowTypeDao().SelectByFlowId(base.Conn, num);
                if (table2.Rows.Count > 0)
                {
                    ListItem item;
                    this.ViewState["SortId"] = Convert.ToInt32(table2.Rows[0]["FlowSort"].ToString());
                    bool flag = Convert.ToBoolean(table2.Rows[0]["IsUsed"].ToString());
                    if (flag)
                    {
                        item = new ListItem("固定流程", "1");
                        if (Convert.ToInt32(table2.Rows[0]["FlowType"].ToString()) == 1)
                        {
                            item.Selected = true;
                            this.ddlFlowType.Items.Add(item);
                        }
                        item = new ListItem("自由流程", "2");
                        if (Convert.ToInt32(table2.Rows[0]["FlowType"].ToString()) == 2)
                        {
                            item.Selected = true;
                            this.ddlFlowType.Items.Add(item);
                        }
                        this.ddlFlowType.Attributes.Add("class", "BigStatic");
                    }
                    else
                    {
                        item = new ListItem("固定流程", "1");
                        this.ddlFlowType.Items.Add(item);
                        item = new ListItem("自由流程", "2");
                        this.ddlFlowType.Items.Add(item);
                        this.ddlFlowType.Attributes.Add("class", "BigSelect");
                        this.ddlFlowType.Items.FindByValue(table2.Rows[0]["FlowType"].ToString()).Selected = true;
                    }
                    DataTable table3 = new FlowFormTypeDao().SelectAll(base.Conn);
                    if (flag)
                    {
                        if (table3.Rows.Count > 0)
                        {
                            for (int i = 0; i < table3.Rows.Count; i++)
                            {
                                item = new ListItem(table3.Rows[i]["FormName"].ToString(), table3.Rows[i]["FormId"].ToString());
                                if (Convert.ToInt32(table2.Rows[0]["FormId"].ToString()) == Convert.ToInt32(table3.Rows[i]["FormId"].ToString()))
                                {
                                    item.Selected = true;
                                    this.ddlFormId.Items.Add(item);
                                }
                            }
                        }
                        this.ddlFormId.Attributes.Add("class", "BigStatic");
                    }
                    else
                    {
                        this.ddlFormId.DataSource = table3;
                        this.ddlFormId.DataValueField = "FormId";
                        this.ddlFormId.DataTextField = "FormName";
                        this.ddlFormId.DataBind();
                        this.ddlFormId.Items.FindByValue(table2.Rows[0]["FormId"].ToString()).Selected = true;
                        this.ddlFormId.Attributes.Add("class", "BigSelect");
                    }
                    this.ViewState["FlowType"] = table2.Rows[0]["FlowType"].ToString();
                    this.txtFlowNo.Text = table2.Rows[0]["FlowNo"].ToString();
                    this.txtFlowName.Text = table2.Rows[0]["FlowName"].ToString();
                    this.ddlSortId.Items.FindByValue(table2.Rows[0]["FlowSort"].ToString()).Selected = true;
                    this.ddlFlowDoc.Items.FindByValue(table2.Rows[0]["FlowDoc"].ToString()).Selected = true;
                    if (table2.Rows[0]["FlowDesc"] != DBNull.Value)
                    {
                        this.txtFlowDesc.Text = table2.Rows[0]["FlowDesc"].ToString();
                    }
                    if (table2.Rows[0]["AutoName"] != DBNull.Value)
                    {
                        this.txtAutoName.Text = table2.Rows[0]["AutoName"].ToString();
                    }
                    this.txtAutoNum.Text = table2.Rows[0]["AutoNum"].ToString();
                    this.txtAutoLen.Text = table2.Rows[0]["AutoLen"].ToString();
                    if (this.ddlAutoEdit.Items.FindByValue(table2.Rows[0]["AutoEdit"].ToString()) != null)
                    {
                        this.ddlAutoEdit.Items.FindByValue(table2.Rows[0]["AutoEdit"].ToString()).Selected = true;
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                FlowTypeDao dao4 = new FlowTypeDao();
                FlowTypeData ollO = new FlowTypeData();
                ollO.FlowId = Convert.ToInt32(this.ViewState["FlowId"]);
                ollO.FlowSort = Convert.ToInt32(this.ddlSortId.SelectedValue);
                ollO.FlowNo = Convert.ToInt32(this.txtFlowNo.Text);
                ollO.FlowName = this.txtFlowName.Text;
                ollO.FlowType = Convert.ToInt32(this.ddlFlowType.SelectedValue);
                ollO.FormId = Convert.ToInt32(this.ddlFormId.SelectedValue);
                ollO.FlowDoc = Convert.ToBoolean(this.ddlFlowDoc.SelectedValue);
                if (this.txtFlowDesc.Text != string.Empty)
                {
                    ollO.FlowDesc = this.txtFlowDesc.Text;
                }
                if (this.txtAutoName.Text != string.Empty)
                {
                    ollO.AutoName = this.txtAutoName.Text;
                }
                ollO.AutoNum = Convert.ToInt32(this.txtAutoNum.Text);
                ollO.AutoLen = Convert.ToInt32(this.txtAutoLen.Text);
                ollO.AutoEdit = Convert.ToInt32(this.ddlAutoEdit.SelectedValue);
                dao4.Update(base.Conn, ollO);
                StringBuilder builder = new StringBuilder("<script>\n");
                builder.Append("parent.sort.location.reload();\n");
                builder.Append("location = 'type_list.aspx?sort_id=" + this.ddlSortId.SelectedValue + "';\n");
                builder.Append("</script>\n");
                if (!this.Page.IsClientScriptBlockRegistered("flow_type"))
                {
                    this.Page.RegisterClientScriptBlock("flow_type", builder.ToString());
                }
            }
        }
    }
}
